package com.zhao.service.serviceImpl;

import com.sun.xml.internal.ws.api.pipe.ServerTubeAssemblerContext;
import com.zhao.Others.DateAndTimeObject;
import com.zhao.Others.FileOperationObject;
import com.zhao.Others.GenerateObject;
import com.zhao.dao.DrivingLicenseDao;
import com.zhao.dao.EbickDao;
import com.zhao.dao.StudentDao;
import com.zhao.dao.SuperviseDao;
import com.zhao.pojo.*;
import com.zhao.service.SuperviseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SuperviseServiceImpl implements SuperviseService {

    @Autowired
    private SuperviseDao superviseDao;

    @Autowired
    private EbickDao ebickDao;

    @Autowired
    private DrivingLicenseDao drivingLicenseDao;

    @Autowired
    private StudentDao studentDao;


    /////////////////////////// admin ///////////////////////////////////////

    @Override
    public String adminGetAllSupervises(Admin admin, HttpSession session) {

        List<SuperviseForm> superviseFormList = superviseDao.findAll(admin.getUniversityID());

        List<SuperviseFormImpl> superviseFormList1 = superviseFormToSuperviseFormImpl(superviseFormList);

        //System.out.println(superviseFormList1.get(0));
        session.setAttribute("adminAllSupervises",superviseFormList1);

        return "success";
    }

    @Override
    public String adminSeeOneSuperviseForm(Admin admin, String superviseFormID, HttpSession session) {

        SuperviseForm superviseForm = superviseDao.findByID(superviseFormID);

        if(superviseForm == null){
            System.out.println("举报单号不存在");
        }

        List<SuperviseForm> superviseFormList = new ArrayList<>();
        superviseFormList.add(superviseForm);

        List<SuperviseFormImpl> superviseFormList1 = superviseFormToSuperviseFormImpl(superviseFormList);

        session.setAttribute("adminOneSuperviseForm",superviseFormList1.get(0));

        return "success";
    }


    ///////////////////////////校警///////////////////////////////////////

    @Override
    public String superviserGetSupervise(HttpSession session) {
        Superviser superviser = (Superviser) session.getAttribute("superviser");
        System.out.println(superviser);
        List<SuperviseForm> canBackSuperviseFormList = superviseDao.findSuperviseByFromIDAndStatus(superviser.getSuperviserID(),"待改正");
        if(canBackSuperviseFormList == null){
            System.out.println("待改正");
        }
        session.setAttribute("canBackSuperviseFormList",canBackSuperviseFormList);

        List<SuperviseForm> waitDealFormList = superviseDao.findSuperviseByFromIDAndStatus(superviser.getSuperviserID(),"待受理");
        if(waitDealFormList == null){
            System.out.println("待受理");
        }
        session.setAttribute("waitDealFormList",waitDealFormList);

        List<SuperviseForm> canNotBackSuperviseFormList = superviseDao.findSuperviseByFromIDAndStatus(superviser.getSuperviserID(),"改正待验证");

        if(canNotBackSuperviseFormList == null){
            System.out.println("改正待验证");
        }
        session.setAttribute("canNotBackSuperviseFormList",canNotBackSuperviseFormList);

        //dispute  争议
        List<SuperviseForm> disputeSuperviseList = superviseDao.findSuperviseByFromIDAndStatus(superviser.getSuperviserID(),"有争议待处理");
        if(disputeSuperviseList == null){
            System.out.println("有争议待处理");
        }
        session.setAttribute("disputeSuperviseList",disputeSuperviseList);


        System.out.println(canNotBackSuperviseFormList);


        return "return";
    }

    @Override
    public String superviserUploadSuperviseForm(SuperviseForm superviseForm, MultipartFile file, HttpSession session) {
        Superviser superviser = (Superviser) session.getAttribute("superviser");
        superviseForm.setFromID(superviser.getSuperviserID());
        superviseForm.setUniversityID(superviser.getUniversityID());

        Ebick ebick = ebickDao.findByID(superviseForm.getEbickID(),superviser.getUniversityID());
        if(ebick == null){
            System.out.println("电动车牌照不存在");
            return "电动车牌照不存在";
        }

        superviseForm.setDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());



        //生成举报单号
        superviseForm.setSuperviseID(GenerateObject.getSuperviseID(superviseForm.getEbickID()));

        //图片保存到服务器
        String visitPath = FileOperationObject.savePicture(file,"supervisepictures",superviser.getUniversityID(),superviser.getSuperviserID());
        superviseForm.setImage(visitPath);
        superviseForm.setToStudentID(ebick.getNowOwnerID());
        System.out.println("service uploadSuperviseForm");
        System.out.println(superviseForm);

        int res = superviseDao.add(superviseForm);
        if(res==1){
            return "success";
        }

        return "error";
    }



    //////////////////////辅导员
    @Override
    public String remindStudent(String superviseID, SchoolCounsellor schoolCounsellor) {

        SuperviseForm superviseForm = superviseDao.findByID(superviseID);
        superviseForm.setStatus("待改正");

        int res = superviseDao.update(superviseForm);
        if(res!=1){
            System.out.println("提醒失败");
            return "error";
        }

        return "success";
    }

    @Override
    public String getSuperviseMyStudent(SchoolCounsellor schoolCounsellor,HttpSession session){

        List<DrivingLicenseImpl> drivingLicenseImplList = new ArrayList<>();


        ////方法一  简单查询，复杂逻辑
        ///方法二   简单逻辑，复杂查询！！！！！！！！！！！！！！1

        ///方法一
        //1找到自己的学生
        List<Student> studentList = studentDao.findByUniversityIDAndDepartmentIDAndMajorIDAndEbickAmountBiggerThan(
                schoolCounsellor.getUniversityID(),
                schoolCounsellor.getDepartmentID(),
                schoolCounsellor.getMajorID(),
                0);

        System.out.println("找到 "+studentList.size()+" 个学生");
        System.out.println(studentList);

        //2找到自己学生的  所有电动车
        List<Ebick> allEbickList = ebickDao.findAll(schoolCounsellor.getUniversityID());
        List<Ebick> myStudentEbickList = new ArrayList<>();

        for(int i= 0 ; i<studentList.size() ; i++){

            Student student = studentList.get(i);

            for(int j=0;j<allEbickList.size();j++){

                Ebick ebick = allEbickList.get(j);

                if(student.getStudentID().equals(ebick.getNowOwnerID())){

                    myStudentEbickList.add(ebick);

                }
            }
        }

        //3找到自己学生电车的举报
        List<SuperviseForm> superviseFormList = superviseDao.findAll(schoolCounsellor.getUniversityID());
        List<SuperviseForm> superviseMyStudentList = new ArrayList<>();

        for(int i = 0 ; i<myStudentEbickList.size(); i++){

            String ebickID = myStudentEbickList.get(i).getEbickID();

            for(int j = 0 ; j < superviseFormList.size(); j++){

                if(ebickID.equals(superviseFormList.get(j).getEbickID())){

                    superviseMyStudentList.add(superviseFormList.get(j));
                }

            }
        }


        System.out.println("找到 "+superviseMyStudentList.size() +"个 举报");
        System.out.println(superviseMyStudentList);

        List<SuperviseForm> superviseFormList1 = new ArrayList<>();
        List<SuperviseForm> superviseFormList2 = new ArrayList<>();
        for(int i=0;i<superviseMyStudentList.size();i++){

            if(superviseMyStudentList.get(i).getStatus().equals("待受理")){
                superviseFormList1.add(superviseMyStudentList.get(i));
            }else {
                superviseFormList2.add(superviseMyStudentList.get(i));
            }

        }
        List<SuperviseFormImpl> mySuperviseFormImpleList1 = superviseFormToSuperviseFormImpl(superviseFormList1);
        List<SuperviseFormImpl> mySuperviseFormImpleList2 = superviseFormToSuperviseFormImpl(superviseFormList2);
        System.out.println(superviseFormList1);
        System.out.println(superviseFormList2);
        System.out.println(mySuperviseFormImpleList1);
        System.out.println(mySuperviseFormImpleList2);
        session.setAttribute("mySuperviseFormImpleList1",mySuperviseFormImpleList1);
        session.setAttribute("mySuperviseFormImpleList2",mySuperviseFormImpleList2);
        session.setAttribute("needDealStudentSuperviseNum",mySuperviseFormImpleList1.size());


        return "success";
    }

    private List<SuperviseFormImpl> superviseFormToSuperviseFormImpl(List<SuperviseForm> superviseFormList){

        List<SuperviseFormImpl> superviseFormImplList = new ArrayList<>();

        for(int i=0 ; i<superviseFormList.size() ; i++){

            SuperviseForm superviseForm = superviseFormList.get(i);

            Ebick ebick = ebickDao.findByID(superviseForm.getEbickID(),superviseForm.getUniversityID());
            Student student = studentDao.findByStudentID(ebick.getNowOwnerID(),ebick.getUniversityID());

            SuperviseFormImpl superviseFormImpl = new SuperviseFormImpl();

            superviseFormImpl.setSuperviseID(superviseForm.getSuperviseID());
            superviseFormImpl.setStudentID(student.getStudentID());
            superviseFormImpl.setStudentName(student.getName());
            superviseFormImpl.setEbickID(ebick.getEbickID());
            superviseFormImpl.setSuperviseType(superviseForm.getSuperviseType());
            superviseFormImpl.setDetails(superviseForm.getDetails());
            superviseFormImpl.setImage(superviseForm.getImage());
            superviseFormImpl.setDate(superviseForm.getDateandtime());
            superviseFormImpl.setUniversityID(student.getUniversityID());
            superviseFormImpl.setFromID(superviseForm.getFromID());
            superviseFormImpl.setStatus(superviseForm.getStatus());
            superviseFormImpl.setCorrectImage(superviseForm.getCorrectImage());
            superviseFormImpl.setToStudentID(superviseForm.getToStudentID());
            superviseFormImplList.add(superviseFormImpl);

        }

        return superviseFormImplList;
    }



    /////////////////////////   学生 //////////////////////////////////
    @Override
    public String uploadSuperviseForm(SuperviseForm superviseForm, MultipartFile file, HttpSession session) {

        Student student = (Student)session.getAttribute("student");
        superviseForm.setFromID(student.getStudentID());
        superviseForm.setUniversityID(student.getUniversityID());

        Ebick ebick = ebickDao.findByID(superviseForm.getEbickID(),student.getUniversityID());
        if(ebick == null){
            System.out.println("电动车牌照不存在");
            return "error";
        }
        superviseForm.setToStudentID(ebick.getNowOwnerID());
        superviseForm.setDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        //生成举报单号
        superviseForm.setSuperviseID(GenerateObject.getSuperviseID(superviseForm.getEbickID()));

        //图片保存到服务器
        String visitPath = FileOperationObject.savePicture(file,"supervisepictures",student.getUniversityID(),student.getStudentID());
        superviseForm.setImage(visitPath);
        superviseForm.setToStudentID(ebick.getNowOwnerID());
        System.out.println("service uploadSuperviseForm");
        System.out.println(superviseForm);

        int res = superviseDao.add(superviseForm);
        if(res==1){
            return "success";
        }

        return "error";
    }

    @Override
    public String getMySupervise(HttpSession session) {

        Student student = (Student) session.getAttribute("student");

        List<SuperviseForm> canBackSuperviseFormList = superviseDao.findSuperviseByFromIDAndStatus(student.getStudentID(),"待改正");
        if(canBackSuperviseFormList == null){
            System.out.println("待改正");
        }
        session.setAttribute("canBackSuperviseFormList",canBackSuperviseFormList);

        List<SuperviseForm> waitDealFormList = superviseDao.findSuperviseByFromIDAndStatus(student.getStudentID(),"待受理");
        if(waitDealFormList == null){
            System.out.println("待受理");
        }
        session.setAttribute("waitDealFormList",waitDealFormList);

        List<SuperviseForm> canNotBackSuperviseFormList = superviseDao.findSuperviseByFromIDAndStatus(student.getStudentID(),"改正待验证");

        if(canNotBackSuperviseFormList == null){
            System.out.println("改正待验证");
        }
        session.setAttribute("canNotBackSuperviseFormList",canNotBackSuperviseFormList);

        //dispute  争议
        List<SuperviseForm> disputeSuperviseList = superviseDao.findSuperviseByFromIDAndStatus(student.getStudentID(),"有争议待处理");
        if(disputeSuperviseList == null){
            System.out.println("有争议待处理");
        }
        session.setAttribute("disputeSuperviseList",disputeSuperviseList);


        System.out.println(canNotBackSuperviseFormList);


        return "return";
    }

    @Override
    public String getSuperviseMe(HttpSession session) {

        Student student = (Student) session.getAttribute("student");
        List<SuperviseForm> superviseFormList = superviseDao.findByToStudentIDAndStatus(student.getStudentID(),"待改正");
        if(superviseFormList == null){
            System.out.println("没有被举报");
        }
        session.setAttribute("superviseFormList",superviseFormList);
        if(superviseFormList.size()==0){
            session.setAttribute("superviseFormList",null);
        }

        List<SuperviseForm> correctSuperviseList = superviseDao.findByToStudentIDAndStatus(student.getStudentID(),"改正待验证");
        if(correctSuperviseList == null){
            System.out.println("改正待验证");
        }
        session.setAttribute("correctSuperviseList",correctSuperviseList);
        //dispute  争议
        List<SuperviseForm> disputeSuperviseList = superviseDao.findByToStudentIDAndStatus(student.getStudentID(),"有争议待处理");
        if(disputeSuperviseList == null){
            System.out.println("有争议待处理");
        }
        session.setAttribute("disputeSuperviseList",disputeSuperviseList);

        ////查看被举报数量
        int num = getNumOFSuperviseMe(student);
        session.setAttribute("numOFSuperviseMe",num);

        return "return";
    }

    @Override
    public String uploadCorrectSupervise(MultipartFile correctImage, HttpSession session) {

        SuperviseForm superviseForm = (SuperviseForm)session.getAttribute("needCorrectSupervise");

        superviseForm.setStatus("改正待验证");
        superviseForm.setCorrectDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());

        Student student = (Student)session.getAttribute("student");
        if(student == null){
            System.out.println("请先登录");
            return "error";
        }
        String visitPath = FileOperationObject.savePicture(correctImage,"correctpicture",student.getUniversityID(),student.getStudentID());
        superviseForm.setCorrectImage(visitPath);
        //superviseForm.setStatus("已改正");

        int res = superviseDao.update(superviseForm);
        if(res == 1){
            System.out.println("success");
            return "success";
        }
        System.out.println("error");
        return "error";
    }

    @Override
    public String uploadAppealSupervise(String appealReason, HttpSession session) {

        SuperviseForm superviseForm = (SuperviseForm)session.getAttribute("appealSupervise");

        superviseForm.setStatus("有争议待处理");
        superviseForm.setAppealDateandtime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        superviseForm.setAppealReason(appealReason);

        int res = superviseDao.update(superviseForm);

        if(res==1){
            System.out.println("success");
            return "success";
        }
        System.out.println("error");
        return "error";
    }

    @Override
    public String deleteSupervise(String superviseID, HttpSession session) {

        Student student = (Student)session.getAttribute("student");
        Superviser superviser = (Superviser) session.getAttribute("superviser");
        if(student==null&&superviser==null){
            return "请先登录";
        }
        if(student!=null){
            SuperviseForm superviseForm = superviseDao.findByID(superviseID);
            if(superviseForm == null || !superviseForm.getFromID().equals(student.getStudentID())){
                System.out.println("举报单号不存在");
                return "error";
            }

            int res = superviseDao.deleteByID(superviseID,student.getUniversityID());

            if(res != 1){
                System.out.println("删除失败");
                return "error";
            }
        }else if(superviser!=null){
            System.out.println(superviser);
            SuperviseForm superviseForm = superviseDao.findByID(superviseID);
            if(superviseForm == null || !superviseForm.getFromID().equals(superviser.getSuperviserID())){
                System.out.println("举报单号不存在");
                return "error";
            }

            int res = superviseDao.deleteByID(superviseID,superviser.getUniversityID());

            if(res != 1){
                System.out.println("删除失败");
                return "error";
            }
        }


        return "success";
    }

    @Override
    public int getNumOFSuperviseMe(Student student) {

        int res = 0;

        DrivingLicense drivingLicense = drivingLicenseDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        System.out.println(drivingLicense);
        if(drivingLicense==null){
            return res;
        }
        List<SuperviseForm> superviseFormList = superviseDao.findSuperviseByEbickIDAndStatus(drivingLicense.getEbickID(),"待改正");
        if(superviseFormList == null){
            System.out.println("没有被举报");
        }else {
            res += superviseFormList.size();
        }


        return res;
    }

    @Override
    public String getSupervisesNum(String universityID, HttpSession session) {
        List<SuperviseForm> superviseFormList = superviseDao.findAll(universityID);
        int superviseFormNum = superviseFormList.size();
        session.setAttribute("superviseFormNum",superviseFormNum);
        return "success";
    }

    @Override
    public String getSupervisesNum(SchoolCounsellor schoolCounsellor, HttpSession session) {
        System.out.println("getSupervisesNum");
        List<SuperviseForm> superviseFormList = superviseDao.findAll(schoolCounsellor.getUniversityID());
        int sum = 0;
        for(SuperviseForm superviseForm : superviseFormList){
            System.out.println(superviseForm);
            Student student = studentDao.findByStudentID(superviseForm.getToStudentID(),schoolCounsellor.getUniversityID());
            System.out.println(student);
            if(student.getMajorID().equals(schoolCounsellor.getMajorID())){
                sum++;
            }
        }
        session.setAttribute("superviseFormNum",sum);
        return "success";
    }

    @Override
    public String getSupervisesNum(Student student, HttpSession session) {
        List<SuperviseForm> superviseFormList = superviseDao.findByToStudentID(student.getStudentID());
        session.setAttribute("superviseFormNum",superviseFormList.size());
        return "success";
    }
}
